package com.example.testandroid;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

public class TargetBgView extends View {
    private int mRingCount = 10;
    private Paint mPaint;

    
    public TargetBgView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        initPaint();
    }

    public TargetBgView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initPaint();
    }

    public TargetBgView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initPaint();
    }

    // can only constructor in java code
    public TargetBgView(Context context, int ringCount) {
        super(context);
        initPaint();
        mRingCount = ringCount;
    }

    private void initPaint() {
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
    }

    private int[] getCenter() {
        int [] centerPoint = new int[2];
        centerPoint[0] = getLeft() + getMeasuredWidth() / 2;
        centerPoint[1] = getTop() + getMeasuredHeight() / 2;
        return centerPoint;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int center[] = getCenter();
        float ringWidth = (float)getMeasuredHeight() / 2 / mRingCount;
        for (int index = mRingCount; index >= 1; index--) {
            if (index % 2 == 0) {
                mPaint.setColor(Color.WHITE);
            } else {
                mPaint.setColor(Color.RED);
            }
            canvas.drawCircle(center[0], center[1], ringWidth * index, mPaint);
        }
        canvas.save();
    }
}
